package com.castle.fortress.admin.study.exam.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.exam.entity.StudyMemberExamEntity;
import com.castle.fortress.admin.study.exam.dto.StudyMemberExamDto;
import com.castle.fortress.admin.study.exam.mapper.StudyMemberExamMapper;
import com.castle.fortress.admin.study.exam.service.StudyMemberExamService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 用户试卷 服务实现类
 *
 * @author castle
 * @since 2023-02-16
 */
@Service
public class StudyMemberExamServiceImpl extends ServiceImpl<StudyMemberExamMapper, StudyMemberExamEntity> implements StudyMemberExamService {
    /**
    * 获取查询条件
    * @param studyMemberExamDto
    * @return
    */
    public QueryWrapper<StudyMemberExamEntity> getWrapper(StudyMemberExamDto studyMemberExamDto){
        QueryWrapper<StudyMemberExamEntity> wrapper= new QueryWrapper();
        if(studyMemberExamDto != null){
            StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
            wrapper.eq(studyMemberExamEntity.getId() != null,"id",studyMemberExamEntity.getId());
            wrapper.eq(studyMemberExamEntity.getRecordId() != null,"record_id",studyMemberExamEntity.getRecordId());
            wrapper.eq(studyMemberExamEntity.getExamId() != null,"exam_id",studyMemberExamEntity.getExamId());
            wrapper.eq(studyMemberExamEntity.getMemberId() != null,"member_id",studyMemberExamEntity.getMemberId());
            wrapper.eq(studyMemberExamEntity.getExamQuestionId() != null,"exam_question_id",studyMemberExamEntity.getExamQuestionId());
            wrapper.eq(StrUtil.isNotEmpty(studyMemberExamEntity.getQuestionType()),"question_type",studyMemberExamEntity.getQuestionType());
            wrapper.eq(studyMemberExamEntity.getScore() != null,"score",studyMemberExamEntity.getScore());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getTitle()),"title",studyMemberExamEntity.getTitle());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getTitleImg()),"title_img",studyMemberExamEntity.getTitleImg());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getTitleMp3()),"title_mp3",studyMemberExamEntity.getTitleMp3());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getOptionType()),"option_type",studyMemberExamEntity.getOptionType());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnalysis()),"analysis",studyMemberExamEntity.getAnalysis());
            wrapper.like(studyMemberExamEntity.getSort() != null,"sort",studyMemberExamEntity.getSort());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswer()),"answer",studyMemberExamEntity.getAnswer());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerA()),"answer_a",studyMemberExamEntity.getAnswerA());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerB()),"answer_b",studyMemberExamEntity.getAnswerB());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerC()),"answer_c",studyMemberExamEntity.getAnswerC());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerD()),"answer_d",studyMemberExamEntity.getAnswerD());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerE()),"answer_e",studyMemberExamEntity.getAnswerE());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getAnswerF()),"answer_f",studyMemberExamEntity.getAnswerF());
            wrapper.like(StrUtil.isNotEmpty(studyMemberExamEntity.getChoiceAnswer()),"choice_answer",studyMemberExamEntity.getChoiceAnswer());
            wrapper.like(studyMemberExamEntity.getStatus() != null,"status",studyMemberExamEntity.getStatus());
            wrapper.like(studyMemberExamEntity.getCreateUser() != null,"create_user",studyMemberExamEntity.getCreateUser());
            wrapper.like(studyMemberExamEntity.getCreateTime() != null,"create_time",studyMemberExamEntity.getCreateTime());
            wrapper.like(studyMemberExamEntity.getUpdateUser() != null,"update_user",studyMemberExamEntity.getUpdateUser());
            wrapper.like(studyMemberExamEntity.getUpdateTime() != null,"update_time",studyMemberExamEntity.getUpdateTime());
            wrapper.like(studyMemberExamEntity.getIsDeleted() != null,"is_deleted",studyMemberExamEntity.getIsDeleted());
        }
        wrapper.orderByAsc("sort");
        return wrapper;
    }


    @Override
    public IPage<StudyMemberExamDto> pageStudyMemberExam(Page<StudyMemberExamDto> page, StudyMemberExamDto studyMemberExamDto) {
        QueryWrapper<StudyMemberExamEntity> wrapper = getWrapper(studyMemberExamDto);
        Page<StudyMemberExamEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyMemberExamEntity> studyMemberExamPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyMemberExamDto> pageDto = new Page(studyMemberExamPage.getCurrent(), studyMemberExamPage.getSize(),studyMemberExamPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyMemberExamPage.getRecords(),StudyMemberExamDto.class));
        return pageDto;
    }

    @Override
    public IPage<StudyMemberExamDto> pageStudyMemberExamExtends(Page<StudyMemberExamDto> page, StudyMemberExamDto studyMemberExamDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
        List<StudyMemberExamEntity> studyMemberExamList=baseMapper.extendsList(pageMap,studyMemberExamEntity);
        Long total = baseMapper.extendsCount(studyMemberExamEntity);
        Page<StudyMemberExamDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyMemberExamList,StudyMemberExamDto.class));
        return pageDto;
    }
    @Override
    public StudyMemberExamDto getByIdExtends(Long id){
        StudyMemberExamEntity  studyMemberExamEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(studyMemberExamEntity,StudyMemberExamDto.class);
    }

    @Override
    public List<StudyMemberExamDto> listStudyMemberExam(StudyMemberExamDto studyMemberExamDto){
        QueryWrapper<StudyMemberExamEntity> wrapper = getWrapper(studyMemberExamDto);
        List<StudyMemberExamEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyMemberExamDto.class);
    }
}

